import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:oktoast/oktoast.dart';
import 'package:provider/provider.dart';

import '../common/change_notifier.dart';
import '../common/global.dart';
import '../generated/l10n.dart';
import 'index_page.dart';

class MyApp extends StatelessWidget with WidgetsBindingObserver {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {

    return OKToast(child: MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => ThemeModel()),
        ChangeNotifierProvider(create: (_) => LocaleModel()),
        ChangeNotifierProvider(create: (_) => UserModel()),
      ],
      child: Consumer2<ThemeModel, LocaleModel>(
        builder: (context, themeModel, localeModel, child) {
          SystemChrome.setSystemUIOverlayStyle(
            SystemUiOverlayStyle(
              statusBarColor: themeModel.isDark ? Colors.black : Colors.white,
              statusBarIconBrightness: themeModel.isDark ? Brightness.light : Brightness.dark,
            )
          );
          return MaterialApp(
            theme: ThemeData(
              useMaterial3: true,
              colorScheme: themeModel.isDark ? const ColorScheme.dark(
                primary: Colors.black54,
                onPrimary: Colors.white54,
                secondary: Colors.white30,
                onSecondary: Colors.white70,
                surface: MaterialColor(0x00050505, {}),
                onSurface: Colors.white54,
              ) : const ColorScheme.light(
                  primary: Colors.green,
                  onPrimary: Colors.black54,
                  secondary: Colors.black38,
                  onSecondary: Colors.black87,
                  surface: MaterialColor(0xFFF5F5F5, {}),
                  onSurface: Colors.black54,
              ),
            ),
            locale: localeModel.getLocale(),
            localizationsDelegates: const [
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
              S.delegate
            ],
            supportedLocales: S.delegate.supportedLocales,
            localeResolutionCallback: (curLocale, supportedLocales) {
              if (localeModel.getLocale() != null) return localeModel.getLocale();
              Locale? locale = curLocale != null && supportedLocales.contains(Locale.fromSubtags(languageCode: curLocale.languageCode)) ?
                  curLocale : const Locale('en', 'US');
              return locale;
            },
            onGenerateTitle: (context) => S.of(context).title,
            home: const IndexPage(),
            // routes: <String, WidgetBuilder>{
            //   'home': (context) => const IndexPage(),
            //   'note_add': (context) => const NoteAddPage(),
            // },
          );
        },
      )
    ));
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.paused) {
      Global.dispose();
    }
    super.didChangeAppLifecycleState(state);
  }

}
